package date;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: 时间相关的操作
 * @author: apple
 * @date:  2021/12/11 3:06 下午
 * @version 1.0
 **/
public class DateTest {

    @Test
    public void test5() {
        Date d = new Date();
        System.out.println(d);

        Date d2 = new Date(2022 - 1900, 12 - 1, 11);
        System.out.println(d2);
    }


    @Test
    public void test4() {

        // 2021-1-1 转换为sql.date
        String s = "2021-1-1";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = format.parse(s);
            System.out.println(parse);
            java.sql.Date date = new java.sql.Date(parse.getTime());
            System.out.println(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {
        SimpleDateFormat format = new SimpleDateFormat();
        String format1 = format.format(new Date()); // 日期转字符串
        System.out.println(format1);

        Date parse = null;
        try {
            parse = format.parse("21-12-11 下午3:46");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(parse);


    }

    @Test
    public void test2(){

        // 对应当前时间的日期对象
        Date date1 = new Date();
        System.out.println(date1.toString());
        System.out.println(date1.getTime());

        java.sql.Date date2 = new java.sql.Date(System.currentTimeMillis());
        System.out.println(date2);
        date1 = date2; // 子类转父类，多态

        Date d3 = new Date(); // 父类
        java.sql.Date d4 = new java.sql.Date(System.currentTimeMillis()); //子类
        d4 = (java.sql.Date) d3; //多态才能强转,报错

        d3 = d4;
        d4 = (java.sql.Date) d3; //多态才能强转


    }

    @Test
    public void test1() {

        // 获取1970-1-1到现在的时间搓
        long l = System.currentTimeMillis();

        System.out.println(l);

    }
}
